package com.koron.operation.classes.form.bean.dto;

import cn.hutool.core.util.NumberUtil;
import com.koron.operation.classes.form.bean.constants.DateDuration;
import com.koron.operation.classes.form.bean.po.ClassesFormShift;
import com.koron.operation.classes.form.bean.po.ClassesFormTakeover;
import com.koron.operation.classes.form.bean.vo.ClassesFormChangeVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author jzp
 * @since 2023/5/27
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(value = "月度排班班次DTO", description = "ClassesFormShiftDTO")
public class ClassesFormShiftDTO extends ClassesFormShift {

    @ApiModelProperty("历史变更")
    private List<ClassesFormChangeVO> changeList;

    @ApiModelProperty("当前人作为接班人的变更信息")
    private List<ClassesFormTakeover> takeoverList;

    /**
     * 排班表初始化完成后的初始工时
     * A/B/C 只算A
     */
    @ApiModelProperty("原始工时")
    private Double planHours;

    /**
     * A/B/C 算B/C
     * +changeList中的加班
     * +takeoverList中的加班
     */
    @ApiModelProperty("加班工时")
    private Double overHours;

    /**
     * planHours+overHours
     */
    @ApiModelProperty("总工时")
    private Double totalHours;

    public Double getTotalHours() {
        return NumberUtil.add(planHours, overHours);
    }

    public List<DateDuration> getDutyTime() {
        List<DateDuration> result = new ArrayList<>();

        // 1. 获取原始上班时间
        result.add(new DateDuration(getClassesStartTime(), getClassesEndTime()));

        if (takeoverList != null) {
            takeoverList.stream()
                    .max(Comparator.comparing(ClassesFormTakeover::getCreateTime))
                    .ifPresent(e -> {
                        if (Objects.equals("0", e.getChangeType())) {
                            result.add(new DateDuration(e.getClassesStartTimeAfter(), e.getClassesEndTimeAfter()));
                        }else if (Objects.equals("3", e.getChangeType())) {
                            result.clear();
                            result.add(new DateDuration(e.getClassesStartTimeAfter(), e.getClassesEndTimeAfter()));
                        }
                    });
        } else if (changeList != null) {
            changeList.stream()
                    .max(Comparator.comparing(ClassesFormChangeVO::getCreateTime))
                    .ifPresent(e -> {
                        if (Objects.equals("0", e.getChangeType())) {
                            result.add(new DateDuration(e.getClassesStartTimeAfter(), e.getClassesEndTimeAfter()));
                        } else if (Objects.equals("1", e.getChangeType())) {
                            result.clear();
                        } else if (Objects.equals("3", e.getChangeType())) {
                            result.clear();
                            result.add(new DateDuration(e.getClassesStartTimeAfter(), e.getClassesEndTimeAfter()));
                        }
                    });
        }
        return result;
    }
}
